﻿using HttpPlugin;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace SMes.DbMethod.Http
{
     public class SmesHttpClients:HttpHelper
     {
        private readonly IServiceScope _serviceScope;
        public SmesHttpClients(HttpClient clientService, IServiceScopeFactory serviceScope, IConfiguration configuration)
            : base(clientService)
        {
            _serviceScope = serviceScope.CreateScope();
            var ApiConfig = new HttpApiConfig();
            try
            {
                ApiConfig = configuration.GetSection("HttpSendApiSetting").Get<HttpApiConfig>();
            }
            catch (Exception ex)
            {
                VolPro.Core.Services.Logger.Error("初始化HTTP错误" + ex.Message);
            }
            finally
            {
                SetApiConfig(ApiConfig);
            }

        }
        public override void SetApiConfig(HttpApiConfig? _apiConfig)
        {
            base.SetApiConfig(_apiConfig);
        }
        public override async Task<bool> AddHttpSendRecord(HttpResultRecord records, bool ignore = false)
        {
            if (string.IsNullOrEmpty(records.ExMessage))
            {
                VolPro.Core.Services.Logger.Info(records.ToString());

            }
            else
            {
                await Task.Delay(1);
                VolPro.Core.Services.Logger.Error(records.ToString());
            }
            //var apiRespository = _serviceScope.ServiceProvider.GetRequiredService<IApiRespository>();
            //await apiRespository.AddAsync(new ApiRecord
            //{
            //    UserName = records.UserName,
            //    Path = records.Path,
            //    RequireWay = records.RequireWay,
            //    SendData = records.SendData,
            //    ResponseData = records.ResponseData,
            //    Success = records.Success,
            //    ExMessage = records.ExMessage,
            //});
            return true;
        }
    }
}
